package pos.gengshen.com.gspos.activity;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.zxing.other.BeepManager;



import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


import pos.gengshen.com.gspos.R;
import pos.gengshen.com.gspos.common.Constant;
import pos.gengshen.com.gspos.utils.HttpUtils;

public class MainActivity extends AppCompatActivity{

    String url = Constant.SERVER_URL+"/admin/shift/android/getShift";
    String url3="http://wx.nanningjunda.com/admin/shift/getPrintShiftData";
    String orderOverUrl = Constant.SERVER_URL+"/admin/order/orderOver";
    private TextView tv;
    Button giftOrderOver,intagary,carexcheck,qrbtn,integralbtn;
    private BeepManager mBeepManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = (Button)findViewById(R.id.button);
        Button btn2 = (Button)findViewById(R.id.button2);
        giftOrderOver = (Button)findViewById(R.id.giftOderOver);
        integralbtn = (Button)findViewById(R.id.integral);
        qrbtn = (Button)findViewById(R.id.qrbtn);
        mBeepManager = new BeepManager(this, R.raw.beep);
        integralbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                Intent intent = new Intent(MainActivity.this,IntegralActivity.class);
                startActivity(intent);

            }
        });





        giftOrderOver.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (checkPackage("com.telpo.tps550.api")) {
                    Intent intent = new Intent();
                    intent.setClassName("com.telpo.tps550.api", "com.telpo.tps550.api.barcode.Capture");
                    try {
                        startActivityForResult(intent, 0x124);
                    } catch (ActivityNotFoundException e) {
                        Toast.makeText(MainActivity.this,"系统错误",
                                Toast.LENGTH_LONG).show();
                    }
                } else {
                    Toast.makeText(MainActivity.this, "系统错误2",
                            Toast.LENGTH_LONG).show();
                }





            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Map<String,String> map = new HashMap<>();
                map.put("name","POST");

                Intent intent = new Intent(MainActivity.this,PrinterActivity.class);
                startActivity(intent);

            }
        });



        qrbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String url = Constant.SERVER_URL+"/admin/car/getCarTypeByCompanyId";
                Map<String,Object> map = new HashMap<>();
                map.put("companyId",1);
                HttpUtils.doGet(url, null, map, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Toast.makeText(MainActivity.this,"请求数据出错",Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        final String result = response.body().string();
                        JSONObject object =JSON.parseObject(result);
                        if(!object.getString("code").equals("200")){
                            return;
                        }
                        final String str = object.getString("obj");
//                        runOnUiThread(new Runnable() {
//                            @Override
//                            public void run() {
//                                Toast.makeText(MainActivity.this,"请求数据"+str,Toast.LENGTH_LONG).show();
//                            }
//                        });
                        Intent intent2 = new Intent(MainActivity.this,QRCodeActivity.class);
                        intent2.putExtra("cartypelist",str);
                        startActivity(intent2);
                    }
                });


                //Toast.makeText(MainActivity.this,"点击了认证按钮",Toast.LENGTH_LONG).show();

            }
        });




        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String result = null;
                HttpUtils.doGet(url, null, null, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        System.out.println("请求失败");
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(),"请求网络失败",Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        final String res = response.body().string();
                        Intent intent = new Intent(MainActivity.this,WorkActivity.class);
                        intent.putExtra("shift",res);
                        startActivity(intent);
                    }
                });
            }


        });


    }

    private boolean checkPackage(String packageName) {
        PackageManager manager = this.getPackageManager();
        Intent intent = new Intent().setPackage(packageName);
        List<ResolveInfo> infos = manager.queryIntentActivities(intent, 0);
        if (infos == null || infos.size() < 1) {
            return false;
        }
        return true;
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0x124) {
            if (resultCode == 0) {
                if (data != null) {
                    mBeepManager.playBeepSoundAndVibrate();
                    try {
                        String qrcode = new String(data.getStringExtra("qrCode").getBytes("ISO-8859-1"), "GB2312");
                        //Toast.makeText(MainActivity.this, "扫码结果:" + qrcode, Toast.LENGTH_LONG).show();
                        Map<String,String> map = new HashMap<>();
                        map.put("orderCode",qrcode);
                        HttpUtils.doPost(orderOverUrl, null, map, new Callback() {
                            @Override
                            public void onFailure(Call call, IOException e) {

                                Toast.makeText(MainActivity.this, "网络错误", Toast.LENGTH_LONG).show();
                            }

                            @Override
                            public void onResponse(Call call, Response response) throws IOException {
                                final String result = response.body().string();
                                if(result != null){
                                    JSONObject object = JSON.parseObject(result);
                                    String code = object.getString("code");
                                    Boolean obj = object.getBoolean("obj");
                                    if(code.equals("200") && obj){

                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                Toast.makeText(MainActivity.this, "礼品核销成功", Toast.LENGTH_LONG).show();
                                            }
                                        });
                                    }
                                }
                            }
                        });
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }

                    return;
                }
            } else {
                Toast.makeText(MainActivity.this, "Scan Failed", Toast.LENGTH_LONG).show();
            }
        }

    }






}
